package com.example.sherlockzhong.plush_v2.Models;

import com.example.sherlockzhong.plush_v2.Constant;
import com.example.sherlockzhong.plush_v2.Constants.UrlConstant;
import com.example.sherlockzhong.plush_v2.Utils.DateTimeUtil;
import com.example.sherlockzhong.plush_v2.Utils.NetworkUtils;
import com.example.sherlockzhong.plush_v2.Utils.StorageUtils;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.HashMap;

/**
 * Created by SherlockZhong on 9/9/14.
 */

public class ClassesModel {

    public static boolean updateClass() {
        try {
            String classes = getClasses();
            if (classes != null) {
                return saveClasses(classes);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String getClasses() {
        if (Constant.STUDENT_ID != null && Constant.STUDENT_TOKEN != null) {
            try {
                String classesJson = NetworkUtils.httpGet(String.format(UrlConstant.classesUrl, Constant.STUDENT_ID, Constant.STUDENT_TOKEN));
                try {
                    JSONTokener jsonTokener = new JSONTokener(classesJson);
                    JSONArray jsonArray = (JSONArray) jsonTokener.nextValue();
                    JSONArray allJsonArray = new JSONArray();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONArray innerJsonArray = (JSONArray) jsonArray.get(i);
                        JSONArray dayJsonArray = new JSONArray();
                        for (int j = 0; j < innerJsonArray.length(); j++) {
                            JSONObject jsonObject = (JSONObject) innerJsonArray.get(j);
                            String className = jsonObject.getString("class_name");
                            if (!className.equalsIgnoreCase("empty")) {
                                String classTime = jsonObject.getString("time");
                                HashMap<String, String> decodedClassTime = DateTimeUtil.decodeClassTime(classTime, i, j);
                                jsonObject.put("weeks", decodedClassTime.get("weeks"));
                                jsonObject.put("weeks_sumary", decodedClassTime.get("weeksSumary"));
                                jsonObject.put("weekday", decodedClassTime.get("weekday"));
                                jsonObject.put("weekday_index", decodedClassTime.get("weekdayIndex"));
                                jsonObject.put("class_order", decodedClassTime.get("classOrder"));
                                if (jsonObject.has("sub_class")) {
                                    JSONTokener subJsonTokener = new JSONTokener(jsonObject.getString("sub_class"));
                                    JSONObject subJsonObject = (JSONObject) subJsonTokener.nextValue();
                                    String subClassTime = subJsonObject.getString("time");
                                    HashMap<String, String> decodedSubClassTime = DateTimeUtil.decodeClassTime(subClassTime, i, j);
                                    subJsonObject.put("weeks", decodedSubClassTime.get("weeks"));
                                    subJsonObject.put("weeks_sumary", decodedSubClassTime.get("weeksSumary"));
                                    subJsonObject.put("weekday", decodedSubClassTime.get("weekday"));
                                    subJsonObject.put("weekday_index", decodedSubClassTime.get("weekdayIndex"));
                                    subJsonObject.put("class_order", decodedSubClassTime.get("classOrder"));
                                    jsonObject.put("sub_class", subJsonObject.toString());
                                }
                            }
                            dayJsonArray.put(jsonObject);
                        }
                        allJsonArray.put(dayJsonArray);
                    }
                    return allJsonArray.toString();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static boolean saveClasses(String classes) {
        return StorageUtils.saveAsFile("Classes.json", classes);
    }

    public static String retrieveClasses() {
        return StorageUtils.retrieveFromFile("Classes.json");
    }

    public static JSONArray findAll() {
        JSONArray allClasses = new JSONArray();
        for (int i = 0; i < 7; i++) {
            allClasses.put(findByDay(i));
        }
        return allClasses;
    }

    public static JSONArray findByDay(int index) {
        String classesJson = retrieveClasses();
        try {
            JSONTokener jsonTokener = new JSONTokener(classesJson);
            JSONArray jsonArray = (JSONArray) jsonTokener.nextValue();
            return (JSONArray) jsonArray.get(index);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JSONObject findByClassOrder(int dayIndex, int classIndex) {
        JSONArray classesOfDay = findByDay(dayIndex);
        try {
            return (JSONObject) classesOfDay.get(classIndex);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
